from elasticsearch import Elasticsearch
import json_pac

class Elasticdemo:
 def __init__(self,index_name,index_type,ip,port):
  self.index_name = index_name
  self.index_type = index_type
  self.ip = ip
  self_port = port
  self.conn = Elasticsearch([{'host':ip,'port':port}])
  if self.conn.ping():
   print('Connecting.')
  else:
   print('Something Error.')

 def info(self):
  print self.conn.info()

 def close(self):
  if self.conn is not None:
   try:
    self.conn.close()
   except Exception, e:
    print e.reason
   finally:
    self.conn = None

 def insert_single(self,data,id_num=1):
   res = self.conn.index(index=self.index_name,doc_type=self.index_type,body=data,id=id_num)
   return json_pac.packet_message(['operate','status'],['insert',res['result']])

 def insert_by_id(self,list,startnum):
  for data in list:
   result = self.insert_single(data,id_num=startnum)
   #print str(result)+" id:"+str(startnum)+". data:"+str(data)
   print json_pac.packet_message(['message'],[result])
   startnum += 1

 def get_single(self,id_num):
  try:
   res = self.conn.get(index=self.index_name,doc_type=self.index_type,id=id_num)
   return json_pac.packet_message(['operate','data'],['search',res['_source']])
  except:
   return json_pac.packet_message(['operate','message'],['search',"Search Failed.Error:id"+"="+str(id_num)+" may out of index or other situation."])

 def get_by_id(self,num1,num2):
  for i in range(num1,num2+1):
   result = self.get_single(i)
   print json_pac.packet_message(['id','message'],[str(i),str(result)])

 def search_by_query(self,query):
  try:
   res = self.conn.search(index=self.index_name,doc_type=self.index_type,body=query)
  except:
   print json_pac.packet_message(['message'],["Search Failed.Error:query"+"="+str(query)+" may has something wrong."])
  else:
   for element in res['hits']['hits']:
    print json_pac.packet_message(['data'],[element['_source']])

 def delete_single(self,id_num):
  try:
   res = self.conn.delete(index=self.index_name,doc_type=self.index_type,id=id_num)
  except:
   return "Delete Failed.Error:id"+"="+str(id_num)+" may has no data or other situation."
  else:
   if res['_shards']['successful']==1:
    result = 'Delete Successfully.'
    return "index_name:"+self.index_name+" doc_type:"+self.index_type+" id:"+str(id_num)+" "+result
   else:
    result = 'Delete Failed.'
    return "index_name:"+self.index_name+" doc_type:"+self.index_type+" id:"+str(id_num)+" "+result
 

 def delete_by_id_batch(self,num1,num2):
  for i in range(num1,num2+1):
   result = self.delete_single(i)
   print result

 def delete_query(self,query):
  try:
   res =  self.conn.delete_by_query(index=self.index_name,doc_type=self.index_type,body=query)
  except:
   return "Delete Failed.Error:query"+"="+str(query)+" may has no data or other situation."
  else:
   if res['deleted'] == 1:
    return "Delete Successfully."
   else:
    return "Delete Failed."

 #def update_single(self,id_num,data):
  #self.conn.update(index=self.index_name,doc_type=self.index_type,id=id_num,body=data)

 #def update_by_id_batch(self,startnum,list):
  #for data in list:
   #update_single(startnum,data)
   #start += 1

 #def update_query(self,query):
  #self.conn.update_by_query(index=self.index_name,doc_type=self.index_type,body=query)

o = Elasticdemo('test','test-type','192.168.159.131','9200')
o.insert_by_id([{'a':'5','b':'6'},{'a':'0','b':'9'}],1)
#print o.insert_single({'m':'1','n':2},1)
#o.update_by_id_batch(1,2,[{'m':'1','n':2},{'m':'3','n':'4'}])
#o.delete_by_id_batch(1,3)
#o.update_single(1,{'m':'2','n':2})
o.get_by_id(1,3)
#o.delete_by_id_batch(1,3)
#print o.get_single(1)
#print o.delete_single(1)
